<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        
        
        <link rel="shortcut icon" href="../img/favicon.ico">
        <title>web3.eth - Web3.js 0.20官方文档中文版</title>
        <link href="../css/bootstrap-custom.min.css" rel="stylesheet">
        <link href="../css/font-awesome.min.css" rel="stylesheet">
        <link href="../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

        <script src="../js/jquery-1.10.2.min.js" defer></script>
        <script src="../js/bootstrap-3.0.3.min.js" defer></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>

        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">

                <!-- Collapsed navigation -->
                <div class="navbar-header">
                    <!-- Expander button -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="..">Web3.js 0.20官方文档中文版</a>
                </div>

                <!-- Expanded navigation -->
                <div class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li >
                                <a href="https://web3.learnblockchain.cn">首页</a>
                            </li>
                            <li >
                                <a href="..">0.20文档</a>
                            </li>
                            <li >
                                <a href="https://learnblockchain.cn">深入浅出区块链</a>
                            </li>
                            <li >
                                <a href="https://wiki.learnblockchain.cn/images/dapp.png">DAPP视频课程</a>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <a href="#" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> Search
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
                <div class="col-md-3"><div class="bs-sidebar hidden-print affix well" role="complementary">
    <ul class="nav bs-sidenav">
        <li class="main active"><a href="#web3eth">web3.eth</a></li>
            <li><a href="#web3ethdefaultaccount">web3.eth.defaultAccount</a></li>
            <li><a href="#web3ethdefaultblock">web3.eth.defaultBlock</a></li>
            <li><a href="#web3ethsyncing">web3.eth.syncing</a></li>
            <li><a href="#web3ethissyncing">web3.eth.isSyncing</a></li>
            <li><a href="#web3ethcoinbase">web3.eth.coinbase</a></li>
            <li><a href="#web3ethmining">web3.eth.mining</a></li>
            <li><a href="#web3ethhashrate">web3.eth.hashrate</a></li>
            <li><a href="#web3ethgasprice">web3.eth.gasPrice</a></li>
            <li><a href="#web3ethaccounts">web3.eth.accounts</a></li>
            <li><a href="#web3ethblocknumber">web3.eth.blockNumber</a></li>
            <li><a href="#web3ethregister">web3.eth.register</a></li>
            <li><a href="#web3ethunregister">web3.eth.unRegister</a></li>
            <li><a href="#web3ethgetbalance">web3.eth.getBalance</a></li>
            <li><a href="#web3ethgetstorageat">web3.eth.getStorageAt</a></li>
            <li><a href="#web3ethgetcode">web3.eth.getCode</a></li>
            <li><a href="#web3ethgetblock">web3.eth.getBlock</a></li>
            <li><a href="#web3ethgetblocktransactioncount">web3.eth.getBlockTransactionCount</a></li>
            <li><a href="#web3ethgetuncle">web3.eth.getUncle</a></li>
            <li><a href="#web3ethgettransaction">web3.eth.getTransaction</a></li>
            <li><a href="#web3ethgettransactionfromblock">web3.eth.getTransactionFromBlock</a></li>
            <li><a href="#web3ethgettransactionreceipt">web3.eth.getTransactionReceipt</a></li>
            <li><a href="#web3ethgettransactioncount">web3.eth.getTransactionCount</a></li>
            <li><a href="#web3ethsendtransaction">web3.eth.sendTransaction</a></li>
            <li><a href="#web3ethsendrawtransaction">web3.eth.sendRawTransaction</a></li>
            <li><a href="#web3ethsign">web3.eth.sign</a></li>
            <li><a href="#web3ethcall">web3.eth.call</a></li>
            <li><a href="#web3ethestimategas">web3.eth.estimateGas</a></li>
            <li><a href="#web3ethfilter">web3.eth.filter</a></li>
            <li><a href="#web3ethcontract">web3.eth.contract</a></li>
            <li><a href="#web3ethgetcompilers">web3.eth.getCompilers</a></li>
            <li><a href="#web3ethcompilesolidity">web3.eth.compile.solidity</a></li>
            <li><a href="#web3ethcompilelll">web3.eth.compile.lll</a></li>
            <li><a href="#web3ethcompileserpent">web3.eth.compile.serpent</a></li>
            <li><a href="#web3ethnamereg">web3.eth.namereg</a></li>
            <li><a href="#web3ethsendibantransaction">web3.eth.sendIBANTransaction</a></li>
            <li><a href="#web3ethiban">web3.eth.iban</a></li>
    </ul>
</div></div>
                <div class="col-md-9" role="main">

<h1 id="web3eth">web3.eth</h1>
<p>web3.eth包含以太坊区块链数据相关的方法</p>
<h2 id="web3ethdefaultaccount">web3.eth.defaultAccount</h2>
<pre><code>web3.eth.defaultAccount
</code></pre>
<p>设置在使用下述方法时使用的默认账户地址：</p>
<ul>
<li><a href="#web3ethsendtransaction">web3.eth.sendTransaction()</a></li>
<li><a href="#web3ethcall">web3.eth.call()</a></li>
</ul>
<h3 id="_1">可能的取值</h3>
<p><code>String</code>, 20个字节 - 返回我们拥有的地址 <em>默认是</em> <code>undefined</code>.</p>
<h3 id="_2">返回值</h3>
<p><code>String</code>, 20字节的当前设置的默认地址.</p>
<h3 id="_3">示例</h3>
<pre><code class="js">var defaultAccount = web3.eth.defaultAccount;
console.log(defaultAccount); // ''

// 设置默认地址
web3.eth.defaultAccount = '0x8888f1f195afa192cfee860698584c030f4c9db1';
</code></pre>

<hr />
<h2 id="web3ethdefaultblock">web3.eth.defaultBlock</h2>
<pre><code>web3.eth.defaultBlock
</code></pre>
<p>在以下方法中会使用该参数来指定块，不过你可以传入<code>defaultBlock</code>参数来 改变方法的目标块:</p>
<ul>
<li><a href="#web3ethgetbalance">web3.eth.getBalance()</a></li>
<li><a href="#web3ethgetcode">web3.eth.getCode()</a></li>
<li><a href="#web3ethgettransactioncount">web3.eth.getTransactionCount()</a></li>
<li><a href="#web3ethgetstorageat">web3.eth.getStorageAt()</a></li>
<li><a href="#web3ethcall">web3.eth.call()</a></li>
<li><a href="#contract-methods">contract.myMethod.call()</a></li>
<li><a href="#contract-methods">contract.myMethod.estimateGas()</a></li>
</ul>
<h3 id="_4">可能的取值</h3>
<ul>
<li><code>Number</code> - 块号</li>
<li><code>String</code> - <code>"earliest"</code>, 创世块</li>
<li><code>String</code> - <code>"latest"</code>, 最后一个块，区块链的当前最新块，是<em>默认值</em></li>
<li><code>String</code> - <code>"pending"</code>, 当前挖掘中的块，包括其中处于pending状态的交易</li>
</ul>
<h3 id="_5">返回值</h3>
<p><code>Number|String</code> - 查询状态时使用的默认块号.</p>
<h3 id="_6">示例</h3>
<pre><code class="js">var defaultBlock = web3.eth.defaultBlock;
console.log(defaultBlock); // 'latest'

// set the default block
web3.eth.defaultBlock = 231;
</code></pre>

<hr />
<h2 id="web3ethsyncing">web3.eth.syncing</h2>
<pre><code>web3.eth.syncing
// or async
web3.eth.getSyncing(callback(error, result){ ... })
</code></pre>
<p>只读属性，当节点与网络已经同步时返回一个同步对象，否则返回<code>false</code>.</p>
<h3 id="_7">返回值</h3>
<p><code>Object|Boolean</code> - 同步时返回的对象具有如下字段，否则返回<code>false</code>:
   - <code>startingBlock</code>: <code>Number</code> - 同步开始时的块号.
   - <code>currentBlock</code>: <code>Number</code> - 当前已经同步的最后一个块号.
   - <code>highestBlock</code>: <code>Number</code> - 需要同步的最后块号，这是一个估计值.</p>
<h3 id="_8">示例</h3>
<pre><code class="js">var sync = web3.eth.syncing;
console.log(sync);
/*
{
   startingBlock: 300,
   currentBlock: 312,
   highestBlock: 512
}
*/
</code></pre>

<hr />
<h2 id="web3ethissyncing">web3.eth.isSyncing</h2>
<pre><code>web3.eth.isSyncing(callback);
</code></pre>
<p>该函数将在每次同步开始、更新和停止时调用指定的回调函数.</p>
<h3 id="_9">返回值</h3>
<p><code>Object</code> - 返回对象syncing具有如下方法:</p>
<ul>
<li><code>syncing.addCallback()</code>: 添加另一个回调函数, 当节点开始或停止同步时将被调用.</li>
<li><code>syncing.stopWatching()</code>: 停止调用回调函数.</li>
</ul>
<h3 id="_10">回调函数的返回值</h3>
<ul>
<li><code>Boolean</code> - 当同步开始时返回 <code>true</code> 停止时返回<code>false</code> .</li>
<li><code>Object</code> - 在同步过程中将返回syncing对象，有以下字段：</li>
<li><code>startingBlock</code>: <code>Number</code> - 同步开始时的块号.</li>
<li><code>currentBlock</code>: <code>Number</code> - 当前已经同步的最后块号.</li>
<li><code>highestBlock</code>: <code>Number</code> - 预估需要同步的块号.</li>
</ul>
<h3 id="_11">示例</h3>
<pre><code class="js">web3.eth.isSyncing(function(error, sync){
    if(!error) {
        // stop all app activity
        if(sync === true) {
           // we use `true`, so it stops all filters, but not the web3.eth.syncing polling
           web3.reset(true);

        // show sync info
        } else if(sync) {
           console.log(sync.currentBlock);

        // re-gain app operation
        } else {
            // run your app init function...
        }
    }
});
</code></pre>

<hr />
<h2 id="web3ethcoinbase">web3.eth.coinbase</h2>
<pre><code class="js">    // 同步调用：
    web3.eth.coinbase
    // 异步调用：
    web3.eth.getCoinbase(callback(error, result){ ... })
</code></pre>

<p>只读属性，返回接收挖矿回报的账户地址.</p>
<h3 id="_12">返回值</h3>
<p><code>String</code> -节点旳coinbase地址.</p>
<h3 id="_13">示例</h3>
<pre><code class="js">var coinbase = web3.eth.coinbase;
console.log(coinbase); // &quot;0x407d73d8a49eeb85d32cf465507dd71d507100c1&quot;
</code></pre>

<hr />
<h2 id="web3ethmining">web3.eth.mining</h2>
<pre><code>web3.eth.mining
// or async
web3.eth.getMining(callback(error, result){ ... })
</code></pre>
<p>只读属性，返回true或false表示节点是否在挖矿。</p>
<h3 id="_14">返回值</h3>
<p><code>Boolean</code> - 如果节点在挖矿则返回<code>true</code> ，否则返回<code>false</code>.</p>
<h3 id="_15">示例</h3>
<pre><code class="js">var mining = web3.eth.mining;
console.log(mining); // true or false
</code></pre>

<hr />
<h2 id="web3ethhashrate">web3.eth.hashrate</h2>
<pre><code class="js">    // 同步调用：
    web3.eth.hashrate

    // 异步调用：
    web3.eth.getHashrate(callback(error, result){ ... })
</code></pre>

<p>只读属性，返回节点当前每秒可算出的hash数量.</p>
<h3 id="_16">返回值</h3>
<p><code>Number</code> - 每秒可算出的哈希值数量.</p>
<h3 id="_17">示例</h3>
<pre><code class="js">var hashrate = web3.eth.hashrate;
console.log(hashrate); // 493736
</code></pre>

<hr />
<h2 id="web3ethgasprice">web3.eth.gasPrice</h2>
<pre><code>同步调用：
web3.eth.gasPrice
异步调用：
web3.eth.getGasPrice(callback(error, result){ ... })
</code></pre>
<p>只读属性,返回当前的gas价格,这个值由最近几个块的gas价格的中值决定。</p>
<h3 id="_18">返回值</h3>
<p><code>BigNumber</code> - 当前的gas价格的<a href="https://web3.learnblockchain.cn/0.2x.x/#web3js">BigNumber</a>实例，以wei为单位.</p>
<h3 id="_19">示例</h3>
<pre><code class="js">var gasPrice = web3.eth.gasPrice;
console.log(gasPrice.toString(10)); // &quot;10000000000000&quot;
</code></pre>

<hr />
<h2 id="web3ethaccounts">web3.eth.accounts</h2>
<pre><code class="js">    // 同步调用：
    web3.eth.accounts
    // 异步调用：
    web3.eth.getAccounts(callback(error, result){ ... })
</code></pre>

<p>只读属性，返回当前节点持有的帐户列表.</p>
<h3 id="_20">返回值</h3>
<p><code>Array</code> - 节点持有的帐户列表.</p>
<h3 id="_21">示例</h3>
<pre><code class="js">var accounts = web3.eth.accounts;
console.log(accounts); // [&quot;0x407d73d8a49eeb85d32cf465507dd71d507100c1&quot;]
</code></pre>

<hr />
<h2 id="web3ethblocknumber">web3.eth.blockNumber</h2>
<pre><code class="js">    // 同步调用：
    web3.eth.accounts
    // 异步调用：
    web3.eth.getblockNumber(callback(error, result){ ... })
</code></pre>

<p>只读属性，返回当前块号.</p>
<h3 id="_22">返回值</h3>
<p><code>Number</code> - 最新块号.</p>
<h3 id="_23">示例</h3>
<pre><code class="js">var number = web3.eth.blockNumber;
console.log(number); // 2744
</code></pre>

<hr />
<h2 id="web3ethregister">web3.eth.register</h2>
<pre><code>web3.eth.register(addressHexString [, callback])
</code></pre>
<p>该方法未实现。注册指定地址到web3.eth.accounts中，从而将非私钥持有者 账户关联到一个持有的账户，例如合约钱包。</p>
<h3 id="_24">参数</h3>
<ol>
<li><code>addressHexString</code>: <code>String</code> - 要注册的地址字符串.</li>
<li><code>callback</code>: <code>Function</code> - 可选。如果传入该回掉函数，该方法将通过HTTP异步请求.</li>
</ol>
<h3 id="_25">示例</h3>
<pre><code class="js">web3.eth.register(&quot;0x407d73d8a49eeb85d32cf465507dd71d507100ca&quot;)
</code></pre>

<hr />
<h2 id="web3ethunregister">web3.eth.unRegister</h2>
<pre><code> web3.eth.unRegister(addressHexString [, callback])
</code></pre>
<p>该函数未实现。取消一个指定地址的注册。</p>
<h3 id="_26">参数</h3>
<ol>
<li><code>addressHexString</code>:  <code>String</code> - 要取消注册的地址.</li>
<li><code>callback</code>: <code>Function</code> - 可选。当使用回调函数时函数将执行异步HTTP请求.</li>
</ol>
<h3 id="_27">示例</h3>
<pre><code class="js">web3.eth.unregister(&quot;0x407d73d8a49eeb85d32cf465507dd71d507100ca&quot;)
</code></pre>

<hr />
<h2 id="web3ethgetbalance">web3.eth.getBalance</h2>
<pre><code>web3.eth.getBalance(addressHexString [, defaultBlock] [, callback])
</code></pre>
<p>返回链上指定地址的账户余额.</p>
<h3 id="_28">参数</h3>
<ol>
<li><code>addressHexString</code>: <code>String</code> - 要查询余额的地址.</li>
<li><code>defaultBlock</code>: <code>Number|String</code> - (（可选）如果不设置此值，将使用<code>web3.eth.defaultBlock</code>设定的块，否则使用指定的块。</li>
<li><code>callback</code>:  <code>Function</code> - （可选）回调函数，用于支持异步的执行方式。</li>
</ol>
<h3 id="_29">返回值</h3>
<p><code>String</code> - 一个包含给定地址的当前余额的<a href="https://web3.learnblockchain.cn/0.2x.x/#web3js">BigNumber</a>实例，单位为wei.</p>
<h3 id="_30">示例</h3>
<pre><code class="js">var balance = web3.eth.getBalance(&quot;0x407d73d8a49eeb85d32cf465507dd71d507100c1&quot;);
console.log(balance); // instanceof BigNumber
console.log(balance.toString(10)); // '1000000000000'
console.log(balance.toNumber()); // 1000000000000
</code></pre>

<hr />
<h2 id="web3ethgetstorageat">web3.eth.getStorageAt</h2>
<pre><code>web3.eth.getStorageAt(addressHexString, position [, defaultBlock] [, callback])
</code></pre>
<p>获取一个地址指定位置的存储内容.</p>
<h3 id="_31">参数</h3>
<ol>
<li><code>addressHexString</code>: <code>String</code> - 要读取内容的地址</li>
<li><code>position</code>: <code>Number</code> - 存储位置索引.</li>
<li><code>defaultBlock</code>: <code>Number|String</code> - 可选，使用该参数来覆盖<a href="#web3ethdefaultblock">web3.eth.defaultBlock</a>的值.</li>
<li><code>callback</code>: <code>Function</code> - 可选，使用回调函数时，方法将使用异步HTTP请求。</li>
</ol>
<h3 id="_32">返回值</h3>
<p><code>String</code> - 在指定位置的存储内容.</p>
<h3 id="_33">示例</h3>
<pre><code class="js">var state = web3.eth.getStorageAt(&quot;0x407d73d8a49eeb85d32cf465507dd71d507100c1&quot;, 0);
console.log(state); // &quot;0x03&quot;
</code></pre>

<hr />
<h2 id="web3ethgetcode">web3.eth.getCode</h2>
<pre><code>web3.eth.getCode(addressHexString [, defaultBlock] [, callback])
</code></pre>
<p>获取链上指定地址的代码.</p>
<h3 id="_34">参数</h3>
<ol>
<li><code>addressHexString</code>: <code>String</code> - 要获得代码的地址.</li>
<li><code>defaultBlock</code>: <code>Number|String</code> - （可选）如果未传递参数，默认使用web3.eth.defaultBlock定义的块，否则使用指定区块.</li>
<li><code>callback</code>: <code>Function</code> - 回调函数，用于支持异步的方式执行.</li>
</ol>
<h3 id="_35">返回值</h3>
<p><code>String</code> - 回调函数，用于支持异步的方式执行.</p>
<h3 id="_36">示例</h3>
<pre><code class="js">var code = web3.eth.getCode(&quot;0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8&quot;);
console.log(code); // &quot;0x600160008035811a818181146012578301005b601b6001356025565b8060005260206000f25b600060078202905091905056&quot;
</code></pre>

<hr />
<h2 id="web3ethgetblock">web3.eth.getBlock</h2>
<pre><code> web3.eth.getBlock(blockHashOrBlockNumber [, returnTransactionObjects] [, callback])
</code></pre>
<p>返回指定编号或哈希的块.</p>
<h3 id="_37">参数</h3>
<ol>
<li><code>String|Number</code> - 块号或哈希值，或者为以下字符串常量："earliest"、"latest" 或"pending".</li>
<li><code>Boolean</code> - 可选，默认值为false，如果设置为true，返回的快中将包含所有的交易对象, 否则仅返回交易哈希.</li>
<li><code>Function</code> - 可选，当使用回调函数时将采用异步HTTP请求来访问节点.</li>
</ol>
<h3 id="_38">返回值</h3>
<p><code>Object</code> - 块对象:</p>
<ul>
<li><code>number</code>: <code>Number</code> - 块编号.</li>
<li><code>hash</code>: <code>String</code>, 32字节，块哈希，当块处于pending状态时，值为null.</li>
<li><code>parentHash</code>: <code>String</code>, 32字节，父块的哈希.</li>
<li><code>nonce</code>: <code>String</code>, 8字节，生成的POW哈希，块处于pending状态时，值为null.</li>
<li><code>sha3Uncles</code>: <code>String</code>, 32字节， 块中叔伯数据的SHA3值.</li>
<li><code>logsBloom</code>: <code>String</code>, 256字节，块中日志的bloom filter，当块处于pending状态时，值为null.</li>
<li><code>transactionsRoot</code>: <code>String</code>, 32字节，块交易树的根节点.</li>
<li><code>stateRoot</code>: <code>String</code>, 32字节，块最终状态树的根节点.</li>
<li><code>miner</code>: <code>String</code>, 20字节，接收挖矿奖励的矿工地址.</li>
<li><code>difficulty</code>: <code>BigNumber</code> -  块难度.</li>
<li><code>totalDifficulty</code>: <code>BigNumber</code> -  截止到本块的全链总难度.</li>
<li><code>extraData</code>: <code>String</code> - 块中的额外数据.</li>
<li><code>size</code>: <code>Number</code> - 按字节计算的块大小.</li>
<li><code>gasLimit</code>: <code>Number</code> - 本块允许的最大gas消耗量.</li>
<li><code>gasUsed</code>: <code>Number</code> - 块中所有交易使用的gas总量.</li>
<li><code>timestamp</code>: <code>Number</code> - 块校验unix时间戳.</li>
<li><code>transactions</code>: <code>Array</code> - 交易对象数组，或者是32字节的交易哈希值，取决于参数returnTransactionObjects的值.</li>
<li><code>uncles</code>: <code>Array</code> - 叔伯块的哈希数组.</li>
</ul>
<h3 id="_39">示例</h3>
<pre><code class="js">var info = web3.eth.getBlock(3150);
console.log(info);
/*
{
  &quot;number&quot;: 3,
  &quot;hash&quot;: &quot;0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46&quot;,
  &quot;parentHash&quot;: &quot;0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88&quot;,
  &quot;nonce&quot;: &quot;0xfb6e1a62d119228b&quot;,
  &quot;sha3Uncles&quot;: &quot;0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347&quot;,
  &quot;logsBloom&quot;: &quot;0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000&quot;,
  &quot;transactionsRoot&quot;: &quot;0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee&quot;,
  &quot;stateRoot&quot;: &quot;0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb&quot;,
  &quot;miner&quot;: &quot;0x8888f1f195afa192cfee860698584c030f4c9db1&quot;,
  &quot;difficulty&quot;: BigNumber,
  &quot;totalDifficulty&quot;: BigNumber,
  &quot;size&quot;: 616,
  &quot;extraData&quot;: &quot;0x&quot;,
  &quot;gasLimit&quot;: 3141592,
  &quot;gasUsed&quot;: 21662,
  &quot;timestamp&quot;: 1429287689,
  &quot;transactions&quot;: [
    &quot;0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b&quot;
  ],
  &quot;uncles&quot;: []
}
*/
</code></pre>

<hr />
<h2 id="web3ethgetblocktransactioncount">web3.eth.getBlockTransactionCount</h2>
<pre><code>web3.eth.getBlockTransactionCount(hashStringOrBlockNumber [, callback])
</code></pre>
<p>返回指定区块的交易数量.</p>
<h3 id="_40">参数</h3>
<ol>
<li><code>String|Number</code> - Number|String -（可选）如果未传递参数，默认使用web3.eth.defaultBlock定义的块，否则使用指定区块.</li>
<li><code>Function</code> - 回调函数，用于支持异步的方式执行7.</li>
</ol>
<h3 id="_41">返回值</h3>
<p><code>Number</code> - 给定区块的交易数量.</p>
<h3 id="_42">示例</h3>
<pre><code class="js">var number = web3.eth.getBlockTransactionCount(&quot;0x407d73d8a49eeb85d32cf465507dd71d507100c1&quot;);
console.log(number); // 1
</code></pre>

<hr />
<h2 id="web3ethgetuncle">web3.eth.getUncle</h2>
<pre><code>web3.eth.getUncle(blockHashStringOrNumber, uncleNumber [, returnTransactionObjects] [, callback])
</code></pre>
<p>通过索引或Hask返回叔伯块</p>
<h3 id="_43">参数</h3>
<ol>
<li><code>blockHashStringOrNumber</code>:<code>String|Number</code> - 块编号或哈希值，或者字符串常量："earliest"、"latest" 、 "pending".</li>
<li><code>uncleNumber``Number</code> - 叔伯块索引.</li>
<li><code>returnTransactionObjects``Boolean</code> - 可选，默认值为false，如果设置为true，则返回的块中将包含完整的所有交易的完整数据，否则仅返回交易的哈希值.
4.<code>callback</code> <code>Function</code> - 可选，如果使用回调函数，则通过异步http向节点API发送请求。</li>
</ol>
<h3 id="_44">返回值</h3>
<p><code>Object</code> -叔伯块对象，内容参考 <a href="#web3ethgetblock">web3.eth.getBlock()</a>.</p>
<p><strong>提示</strong> ：叔伯块中不包含单个交易.</p>
<h3 id="_45">示例</h3>
<pre><code class="js">var uncle = web3.eth.getUncle(500, 0);
console.log(uncle); // see web3.eth.getBlock

</code></pre>

<hr />
<h2 id="web3ethgettransaction">web3.eth.getTransaction</h2>
<pre><code>web3.eth.getTransaction(transactionHash [, callback])
</code></pre>
<p>返回交易hash对应的交易</p>
<h3 id="_46">参数</h3>
<ol>
<li><code>String</code> - 交易的哈希值.</li>
<li><code>Function</code> - <a href="https://web3.learnblockchain.cn/0.2x.x/#_1">回调函数</a>函数，用于支持异步的方式执行.</li>
</ol>
<h3 id="_47">返回值</h3>
<p><code>Object</code> -一个交易对象 <code>transactionHash</code>:</p>
<ul>
<li><code>hash</code>: <code>String</code>, 32字节，交易的哈希值.</li>
<li><code>nonce</code>: <code>Number</code> - 交易的发起者在之前进行过的交易数量.</li>
<li><code>blockHash</code>: <code>String</code>, 32字节。交易所在区块的哈希值。当这个区块处于pending将会返回null.</li>
<li><code>blockNumber</code>: <code>Number</code> - 交易所在区块的块号。当这个区块处于pending将会返回null.</li>
<li><code>transactionIndex</code>: <code>Number</code> -整数。交易在区块中的序号。当这个区块处于pending将会返回null.</li>
<li><code>from</code>: <code>String</code>, 20字节，交易发起者的地址.</li>
<li><code>to</code>: <code>String</code>, 20字节，交易接收者的地址。当这个区块处于pending将会返回null.</li>
<li><code>value</code>: <code>BigNumber</code> - 交易附带的货币量，单位为Wei.</li>
<li><code>gasPrice</code>: <code>BigNumber</code> - 交易发起者配置的gas价格，单位是wei.</li>
<li><code>gas</code>: <code>Number</code> - 交易发起者提供的gas.</li>
<li><code>input</code>: <code>String</code> - 交易附带的数据.</li>
</ul>
<h3 id="_48">示例</h3>
<pre><code class="js">var transaction = web3.eth.getTransaction('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
console.log(transaction);
/*
{
  &quot;hash&quot;: &quot;0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b&quot;,
  &quot;nonce&quot;: 2,
  &quot;blockHash&quot;: &quot;0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46&quot;,
  &quot;blockNumber&quot;: 3,
  &quot;transactionIndex&quot;: 0,
  &quot;from&quot;: &quot;0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b&quot;,
  &quot;to&quot;: &quot;0x6295ee1b4f6dd65047762f924ecd367c17eabf8f&quot;,
  &quot;value&quot;: BigNumber,
  &quot;gas&quot;: 314159,
  &quot;gasPrice&quot;: BigNumber,
  &quot;input&quot;: &quot;0x57cb2fc4&quot;
}
*/

</code></pre>

<hr />
<h2 id="web3ethgettransactionfromblock">web3.eth.getTransactionFromBlock</h2>
<pre><code>getTransactionFromBlock(hashStringOrNumber, indexNumber [, callback])
</code></pre>
<p>返回交易hash或区块好对应的交易。</p>
<h3 id="_49">参数</h3>
<ol>
<li><code>hashStringOrNumber</code>: <code>String</code> - 区块号或哈希。或者是earliest，latest或pending。查看<a href="#web3.eth.defaultBlock">web3.eth.defaultBlock</a>了解可选值.</li>
<li><code>indexNumber</code>:  <code>Number</code> - 交易的序号.</li>
<li><code>callback</code>: <code>Function</code> -回调函数，用于支持异步的方式执行.</li>
</ol>
<h3 id="_50">返回值</h3>
<p><code>Object</code> - 交易对象，详见<a href="#web3ethgettransaction">web3.eth.getTransaction</a>:</p>
<h3 id="_51">示例</h3>
<pre><code class="js">var transaction = web3.eth.getTransactionFromBlock('0x4534534534', 2);
console.log(transaction); // see web3.eth.getTransaction

</code></pre>

<hr />
<h2 id="web3ethgettransactionreceipt">web3.eth.getTransactionReceipt</h2>
<pre><code>web3.eth.getTransactionReceipt(hashString [, callback])
</code></pre>
<p>指定一个交易哈希，返回一个交易的收据。需要指出的是，处于pending状态的交易，收据是不可用的.</p>
<h3 id="_52">参数</h3>
<ol>
<li><code>String</code> -交易的哈希.</li>
<li><code>Function</code> -回调函数，用于支持异步的方式执行.</li>
</ol>
<h3 id="_53">返回值</h3>
<p><code>Object</code> - 交易的收据对象，如果找不到返回null:</p>
<ul>
<li><code>blockHash</code>: <code>String</code>, 32字节，这个交易所在区块的哈希.</li>
<li><code>blockNumber</code>: <code>Number</code> - 交易所在区块的块号.</li>
<li><code>transactionHash</code>: <code>String</code>, 32字节，交易的哈希值.</li>
<li><code>transactionIndex</code>: <code>Number</code> - 交易在区块里面的序号，整数.</li>
<li><code>from</code>: <code>String</code>,20字节，交易发送者的地址.</li>
<li><code>to</code>: <code>String</code>, 20字节，交易接收者的地址。如果是一个合约创建的交易，返回null.</li>
<li><code>cumulativeGasUsed</code>: <code>Number</code> -当前交易执行后累计花费的gas总值10.</li>
<li><code>gasUsed</code>: <code>Number</code> -   执行当前这个交易单独花费的gas.</li>
<li><code>contractAddress</code>: <code>String</code> -  20字节，创建的合约地址。如果是一个合约创建交易，返回合约地址，其它情况返回null.</li>
<li><code>logs</code>:  <code>Array</code> - 这个交易产生的日志对象数组.</li>
<li><code>status</code>:  <code>String</code> - '0x0' indicates transaction failure , '0x1' indicates transaction succeeded.</li>
</ul>
<h3 id="_54">示例</h3>
<pre><code class="js">var receipt = web3.eth.getTransactionReceipt('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
console.log(receipt);
{
  &quot;transactionHash&quot;: &quot;0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b&quot;,
  &quot;transactionIndex&quot;: 0,
  &quot;blockHash&quot;: &quot;0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46&quot;,
  &quot;blockNumber&quot;: 3,
  &quot;contractAddress&quot;: &quot;0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b&quot;,
  &quot;cumulativeGasUsed&quot;: 314159,
  &quot;gasUsed&quot;: 30234,
  &quot;logs&quot;: [{
         // logs as returned by getFilterLogs, etc.
     }, ...],
  &quot;status&quot;: &quot;0x1&quot;
}
</code></pre>

<hr />
<h2 id="web3ethgettransactioncount">web3.eth.getTransactionCount</h2>
<pre><code>web3.eth.getTransactionCount(addressHexString [, defaultBlock] [, callback])
</code></pre>
<p>返回指定地址发起的交易数.</p>
<h3 id="_55">参数</h3>
<ol>
<li><code>String</code> - 要获得交易数的地址.</li>
<li><code>Number|String</code> - （可选）如果未传递参数，默认使用 <a href="#web3ethdefaultblock">web3.eth.defaultBlock</a>定义的块，否则使用指定区块.</li>
<li><code>Function</code> - 回调函数，用于支持异步的方式执行7.</li>
</ol>
<h3 id="_56">返回值</h3>
<p><code>Number</code> - 指定地址发送的交易数量.</p>
<h3 id="_57">示例</h3>
<pre><code class="js">var number = web3.eth.getTransactionCount(&quot;0x407d73d8a49eeb85d32cf465507dd71d507100c1&quot;);
console.log(number); // 1
</code></pre>

<hr />
<h2 id="web3ethsendtransaction">web3.eth.sendTransaction</h2>
<pre><code>web3.eth.sendTransaction(transactionObject [, callback])
</code></pre>
<p>发送一个交易到网络。如果交易是一个合约创建的，请使用web3.eth.getTransactionReceipt()在交易完成后获取合约的地址。</p>
<h3 id="_58">参数</h3>
<ol>
<li>
<p><code>Object</code> - Object - 要发送的交易对象:</p>
<ul>
<li><code>from</code>: <code>String</code> - 指定的发送者的地址。如果不指定，使用web3.eth.defaultAccount.</li>
<li><code>to</code>: <code>String</code> -（可选）指定的发送者的地址。如果是创建合约，则不需要指定。</li>
<li><code>value</code>: <code>Number|String|BigNumber</code> -（可选）交易携带的货币量，以wei为单位。如果合约创建交易，则为合约初始余额。</li>
<li><code>gas</code>: <code>Number|String|BigNumber</code> - （可选）默认是自动确定，交易可使用的gas，未使用的gas会退回。</li>
<li><code>gasPrice</code>: <code>Number|String|BigNumber</code> - （可选）默认是自动确定，交易的gas价格，默认是网络gas价格的平均值。</li>
<li><code>data</code>: <code>String</code> - （可选）或者包含相关数据的字节字符串，如果是合约创建，则是初始化要用到的代码。</li>
<li><code>nonce</code>: <code>Number</code>  - （可选）整数，使用此值，可以允许你覆盖自己的相同nonce（正在pending中的交易）。</li>
</ul>
</li>
<li>
<p><code>Function</code> - 回调函数，用于支持异步的方式执行.</p>
</li>
</ol>
<h3 id="_59">返回值</h3>
<p><code>String</code> - 32字节的交易哈希串。用16进制表示.</p>
<h3 id="_60">示例</h3>
<pre><code class="js">
// compiled solidity source code using https://chriseth.github.io/cpp-ethereum/
var code = &quot;603d80600c6000396000f3007c01000000000000000000000000000000000000000000000000000000006000350463c6888fa18114602d57005b6007600435028060005260206000f3&quot;;

web3.eth.sendTransaction({data: code}, function(err, transactionHash) {
  if (!err)
    console.log(transactionHash); // &quot;0x7f9fade1c0d57a7af66ab4ead7c2eb7b11a91385&quot;
});
</code></pre>

<hr />
<h2 id="web3ethsendrawtransaction">web3.eth.sendRawTransaction</h2>
<pre><code>web3.eth.sendRawTransaction(signedTransactionData [, callback])
</code></pre>
<p>发送一个已经签名的交易。比如可以用下述签名的例子。
如果交易是一个合约创建，请使用web3.eth.getTransactionReceipt()在交易完成后获取合约的地址。</p>
<h3 id="_61">参数</h3>
<ol>
<li><code>signedTransacionData``String</code> - 16进制格式的签名交易数据.</li>
<li><code>callback``Function</code> - 回调函数，用于支持异步的方式执行.</li>
</ol>
<h3 id="_62">返回值</h3>
<p><code>String</code> - 32字节的16进制格式的交易哈希串.</p>
<h3 id="_63">示例</h3>
<pre><code class="js">var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

var rawTx = {
  nonce: '0x00',
  gasPrice: '0x09184e72a000',
  gasLimit: '0x2710',
  to: '0x0000000000000000000000000000000000000000',
  value: '0x00',
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

//console.log(serializedTx.toString('hex'));
//f889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
  if (!err)
    console.log(hash); // &quot;0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385&quot;
});
</code></pre>

<hr />
<h2 id="web3ethsign">web3.eth.sign</h2>
<pre><code>web3.eth.sign(address, dataToSign [, callback])
</code></pre>
<p>使用指定帐户签名要发送的数据，帐户需要处于解锁状态。</p>
<h3 id="_64">参数</h3>
<ol>
<li><code>String</code> - 签名使用的地址.</li>
<li><code>String</code> - 要签名的数据.</li>
<li><code>Function</code> - （可选）<a href="https://web3.learnblockchain.cn/0.2x.x/#_1">回调</a> 函数，用于支持异步的方式执行.</li>
</ol>
<h3 id="_65">返回值</h3>
<p><code>String</code> - 签名后的数据.</p>
<p>返回的值对应的是ECDSA（Elliptic Curve Digital Signature Algorithm）签名后的字符串:</p>
<pre><code>r = signature[0:64]
s = signature[64:128]
v = signature[128:130]
</code></pre>

<p>需要注意的是，如果你使用ecrecover，这里的v值是00或01，所以如果你想使用他们，你需要把这里的v值转成整数，再加上27。最终你要用的值将是27或2813.</p>
<h3 id="_66">示例</h3>
<pre><code class="js">var result = web3.eth.sign(
    &quot;0x9dd2c369a187b4e6b9c402f030e50743e619301ea62aa4c0737d4ef7e10a3d49&quot;, // first argument is web3.sha3(&quot;xyz&quot;)
    &quot;0x135a7de83802408321b74c322f8558db1679ac20&quot;);
console.log(result); // &quot;0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400&quot;
</code></pre>

<hr />
<h2 id="web3ethcall">web3.eth.call</h2>
<pre><code>web3.eth.call(callObject [, defaultBlock] [, callback])
</code></pre>
<p>执行一个消息调用交易，消息调用直接在节点旳VM中执行，而不会通过挖矿改写区块链。</p>
<h3 id="_67">参数</h3>
<ol>
<li><code>callObject</code>: <code>Object</code> - 交易对象，<a href="web3.eth.sendTransaction">参考</a>, 区别在于对于消息调用而言，from字段是可选的.</li>
<li><code>defaultBlock</code>: <code>Number|String</code> - Number|String，可选，使用该参数覆盖 <a href="#web3ethdefaultblock">web3.eth.defaultBlock</a>的值.</li>
<li><code>callback</code>: <code>Function</code> -可选，当使用<a href="https://web3.learnblockchain.cn/0.2x.x/#_1">回调函数</a> 函数时将通过异步http请求节点API.</li>
</ol>
<h3 id="_68">返回值</h3>
<p><code>String</code> - 调用返回的数据.</p>
<h3 id="_69">示例</h3>
<pre><code class="js">var result = web3.eth.call({
    to: &quot;0xc4abd0339eb8d57087278718986382264244252f&quot;,
    data: &quot;0xc6888fa10000000000000000000000000000000000000000000000000000000000000003&quot;
});
console.log(result); // &quot;0x0000000000000000000000000000000000000000000000000000000000000015&quot;
</code></pre>

<hr />
<h2 id="web3ethestimategas">web3.eth.estimateGas</h2>
<pre><code>web3.eth.estimateGas(callObject [, callback])
</code></pre>
<p>估计调用需要耗费的gas量。这个方法在节点的VM中执行一个消息调用或交易，但是不会修改区块链.</p>
<h3 id="_70">参数</h3>
<p><code>callObject</code>： Object - 要发送的交易对象，可包含以下字段：
  - from: String - 指定的发送者的地址。如果不指定，使用web3.eth.defaultAccount。
  - to: String - （可选）交易消息的目标地址，如果是合约创建，则不填.
  - value: Number|String|BigNumber - （可选）交易携带的货币量，以wei为单位。如果合约创建交易，则为初始的基金。
  - gas: Number|String|BigNumber - （可选）默认是自动，交易可使用的gas，未使用的gas会退回。
  - gasPrice: Number|String|BigNumber - （可选）默认是自动确定，交易的gas价格，默认是网络gas价格的平均值 。
  - data: String - （可选）或者包含相关数据的字节字符串，如果是合约创建，则是初始化要用到的代码。
  - nonce: Number - （可选）整数，使用此值，可以允许你覆盖你自己的相同nonce的，正在pending中的交易。
<code>callback</code>：Function - 回调函数，用于支持异步的执行方式.</p>
<h3 id="_71">返回值</h3>
<p><code>Number</code> - 模拟的call/transcation花费的gas.</p>
<h3 id="_72">示例</h3>
<pre><code class="js">var result = web3.eth.estimateGas({
    to: &quot;0xc4abd0339eb8d57087278718986382264244252f&quot;,
    data: &quot;0xc6888fa10000000000000000000000000000000000000000000000000000000000000003&quot;
});
console.log(result); // &quot;0x0000000000000000000000000000000000000000000000000000000000000015&quot;
</code></pre>

<hr />
<h2 id="web3ethfilter">web3.eth.filter</h2>
<pre><code class="js">// can be 'latest' or 'pending'
var filter = web3.eth.filter(filterString);
// OR object are log filter options
var filter = web3.eth.filter(options);

// watch for changes
filter.watch(function(error, result){
  if (!error)
    console.log(result);
});

// Additionally you can start watching right away, by passing a callback:
web3.eth.filter(options, function(error, result){
  if (!error)
    console.log(result);
});
</code></pre>

<h3 id="_73">参数</h3>
<p><code>String|Object</code> - String|Object - 字符串的可取值[latest，pending]。latest表示监听最新的区块变化，pending表示监听正在pending的区块。如果需要按条件对象过滤，对象字段如下：
- <code>fromBlock</code>: <code>Number|String</code> - 起始区块号（如果使用字符串latest，意思是最新的，正在打包的区块），默认值是latest.
- <code>toBlock</code>: <code>Number|String</code> - 终止区块号（如果使用字符串latest，意思是最新的，正在打包的区块），默认值是latest.
- <code>address</code>: <code>String</code> - 单个或多个地址。获取指定帐户的日志
- <code>topics</code>: <code>Array of Strings</code> - 在日志对象中必须出现的字符串数组。顺序非常重要，如果你想忽略主题，使用null。如，[null,'0x00...']，你还可以为每个主题传递一个单独的可选项数组，如[null,['option1','option1']]</p>
<h3 id="_74">返回值</h3>
<p>Object - 有下述方法的过滤对象：
-   filter.get(callback): 返回满足过滤条件的日志。
-   filter.watch(callback): 监听满足条件的状态变化，满足条件时调用回调。
-   filter.stopWatching(): 停止监听，清除节点中的过滤。你应该总是在监听完成后，执行这个操作.</p>
<h3 id="_75">监听回调的返回值</h3>
<ul>
<li>String - 当使用latest参数时。返回最新的一个区块哈希值。</li>
<li>String - 当使用pending参数时。返回最新的pending中的交易哈希值。</li>
<li>Object - 当使用手工过滤选项时，将返回下述的日志对象。</li>
<li>logIndex: Number - 日志在区块中的序号。如果是pending的日志，则为null。</li>
<li>transactionIndex: Number - 产生日志的交易在区块中的序号。如果是pending的日志，则为null。</li>
<li>transactionHash: String，32字节 - 产生日志的交易哈希值。</li>
<li>blockHash: String，32字节 - 日志所在块的哈希。如果是pending的日志，则为null。</li>
<li>blockNumber: Number - 日志所在块的块号。如果是pending的日志，则为null。</li>
<li>address: String，32字节 - 日志产生的合约地址。</li>
<li>data: string - 包含日志一个或多个32字节的非索引的参数。</li>
<li>topics: String[] - 一到四个32字节的索引的日志参数数组。（在Solidity中，第一个主题是整个事件的签名（如，Deposit(address,bytes32,uint256)），但如果使用匿名的方式定义事件的情况除外） 事件监听器的返回结果，见后合约对象的事件。</li>
</ul>
<p><strong>注意</strong> 事件过滤的返回值参考下文：<a href="#contract-events">合约事件</a></p>
<h3 id="_76">示例</h3>
<pre><code class="js">var filter = web3.eth.filter('pending');

filter.watch(function (error, log) {
  console.log(log); //  {&quot;address&quot;:&quot;0x0000000000000000000000000000000000000000&quot;, &quot;data&quot;:&quot;0x0000000000000000000000000000000000000000000000000000000000000000&quot;, ...}
});

// get all past logs again.
var myResults = filter.get(function(error, logs){ ... });

...

// stops and uninstalls the filter
filter.stopWatching();

</code></pre>

<hr />
<h2 id="web3ethcontract">web3.eth.contract</h2>
<pre><code>web3.eth.contract(abiArray)
</code></pre>
<p>创建一个Solidity的合约对象，用来在某个地址上初始化合约实例。</p>
<h3 id="_77">参数</h3>
<ol>
<li><code>abiArray</code> : <code>Array</code> - 描述合约的函数，事件的ABI对象数组</li>
</ol>
<h3 id="_78">返回值</h3>
<p><code>Object</code> - 一个合约对象，接着就可以使用以下方法来实例化</p>
<pre><code class="js">var MyContract = web3.eth.contract(abiArray);

// 使用合约地址实例化合约
var contractInstance = MyContract.at(address);

// 部署新合约
var contractInstance = MyContract.new([constructorParam1] [, constructorParam2], {data: '0x12345...', from: myAccount, gas: 1000000});

// 获得部署合约数据
var contractData = MyContract.new.getData([constructorParam1] [, constructorParam2], {data: '0x12345...'});
// contractData = '0x12345643213456000000000023434234'
</code></pre>

<p>可以或者使用一个在某个地址上已经存在的合约，或者使用编译后的字节码部署一个全新的的合约。</p>
<pre><code class="js">// Instantiate from an existing address:
var myContractInstance = MyContract.at(myContractAddress);


// Or deploy a new contract:

// 读取合约文件部署合约
...
const fs = require(&quot;fs&quot;);
const solc = require('solc')

let source = fs.readFileSync('nameContract.sol', 'utf8');
let compiledContract = solc.compile(source, 1);
let abi = compiledContract.contracts['nameContract'].interface;
let bytecode = compiledContract.contracts['nameContract'].bytecode;
let gasEstimate = web3.eth.estimateGas({data: bytecode});
let MyContract = web3.eth.contract(JSON.parse(abi));

var myContractReturned = MyContract.new({
   from:mySenderAddress,
   data:bytecode,
   gas:gasEstimate}, function(err, myContract){
    if(!err) {
       // NOTE: 将回调两次
       // Once the contract has the transactionHash property set and once its deployed on an address.

       // e.g. check tx hash on the first call (transaction send)
       if(!myContract.address) {
           console.log(myContract.transactionHash) // The hash of the transaction, which deploys the contract

       // check address on the second call (contract deployed)
       } else {
           console.log(myContract.address) // the contract address
       }

       // Note that the returned &quot;myContractReturned&quot; === &quot;myContract&quot;,
       // so the returned &quot;myContractReturned&quot; object will also get the address set.
    }
  });

// 同步部署合约: The address will be added as soon as the contract is mined.
// Additionally you can watch the transaction by using the &quot;transactionHash&quot; property
var myContractInstance = MyContract.new(param1, param2, {data: myContractCode, gas: 300000, from: mySenderAddress});
myContractInstance.transactionHash // The hash of the transaction, which created the contract
myContractInstance.address // undefined at start, but will be auto-filled later
</code></pre>

<h3 id="_79">示例</h3>
<pre><code class="js">// contract abi
var abi = [{
     name: 'myConstantMethod',
     type: 'function',
     constant: true,
     inputs: [{ name: 'a', type: 'string' }],
     outputs: [{name: 'd', type: 'string' }]
}, {
     name: 'myStateChangingMethod',
     type: 'function',
     constant: false,
     inputs: [{ name: 'a', type: 'string' }, { name: 'b', type: 'int' }],
     outputs: []
}, {
     name: 'myEvent',
     type: 'event',
     inputs: [{name: 'a', type: 'int', indexed: true},{name: 'b', type: 'bool', indexed: false}]
}];

// creation of contract object
var MyContract = web3.eth.contract(abi);

// initiate contract for an address
var myContractInstance = MyContract.at('0xc4abd0339eb8d57087278718986382264244252f');

// call constant function
var result = myContractInstance.myConstantMethod('myParam');
console.log(result) // '0x25434534534'

// send a transaction to a function
myContractInstance.myStateChangingMethod('someParam1', 23, {value: 200, gas: 2000});

// short hand style
web3.eth.contract(abi).at(address).myAwesomeMethod(...);

// create filter
var filter = myContractInstance.myEvent({a: 5}, function (error, result) {
  if (!error)
    console.log(result);
    /*
    {
        address: '0x8718986382264244252fc4abd0339eb8d5708727',
        topics: &quot;0x12345678901234567890123456789012&quot;, &quot;0x0000000000000000000000000000000000000000000000000000000000000005&quot;,
        data: &quot;0x0000000000000000000000000000000000000000000000000000000000000001&quot;,
        ...
    }
    */
});
</code></pre>

<hr />
<h3 id="contract-methods">Contract Methods合约方法</h3>
<pre><code class="js">// Automatically determines the use of call or sendTransaction based on the method type
myContractInstance.myMethod(param1 [, param2, ...] [, transactionObject] [, defaultBlock] [, callback]);

// Explicitly calling this method
myContractInstance.myMethod.call(param1 [, param2, ...] [, transactionObject] [, defaultBlock] [, callback]);

// Explicitly sending a transaction to this method
myContractInstance.myMethod.sendTransaction(param1 [, param2, ...] [, transactionObject] [, callback]);

// Get the call data, so you can call the contract through some other means
// var myCallData = myContractInstance.myMethod.request(param1 [, param2, ...]);
var myCallData = myContractInstance.myMethod.getData(param1 [, param2, ...]);
// myCallData = '0x45ff3ff6000000000004545345345345..'
</code></pre>

<p>合约对象内封装了使用合约的相关方法。可以通过传入参数，和交易对象来使用方法。</p>
<h3 id="_80">参数</h3>
<ul>
<li><code>param1|param2</code>: <code>String|Number|BigNumber</code> - (可选) 零或多个函数参数。如果传入一个字符串，需要使用十六进制编码，如 "0xdeadbeef"，如果已经创建了一个BigNumber对像可以直接传入</li>
<li><code>transactionObject</code>: <code>Object</code> - (可选) 最后一个参数（如果传了callback，则是倒数第二个参数），可以是一个交易对象。参考<a href="#web3ethsendtransaction">web3.eth.sendTransaction</a>的第一个参数。<strong>注意</strong> 这里不需要填data和to属性。</li>
<li><code>defaultBlock</code>: <code>Number|String</code> - (可选) 如果不设置此值使用<a href="#web3ethdefaultblock">web3.eth.defaultBlock</a>设定的块，否则使用指定的块。</li>
<li><code>Function</code> - (可选) 回调函数，用于支持异步的方式执行。</li>
</ul>
<h3 id="_81">返回值</h3>
<p><code>String</code> - 如果发起的是一个call，对应的是返回结果。如果是transaction，则要么是一个创建的合约地址，要么是一个transaction的哈希值。可参考<a href="#web3ethsendtransaction">web3.eth.sendTransaction</a>.</p>
<h3 id="_82">示例</h3>
<pre><code class="js">// creation of contract object
var MyContract = web3.eth.contract(abi);

// initiate contract for an address
var myContractInstance = MyContract.at('0x78e97bcc5b5dd9ed228fed7a4887c0d7287344a9');

var result = myContractInstance.myConstantMethod('myParam');
console.log(result) // '0x25434534534'

myContractInstance.myStateChangingMethod('someParam1', 23, {value: 200, gas: 2000}, function(err, result){ ... });
</code></pre>

<hr />
<h3 id="contract-events">Contract Events合约事件</h3>
<pre><code class="js">var event = myContractInstance.MyEvent({valueA: 23} [, additionalFilterObject])

// watch for changes
event.watch(function(error, result){
  if (!error)
    console.log(result);
});

// Or pass a callback to start watching immediately
var event = myContractInstance.MyEvent([{valueA: 23}] [, additionalFilterObject] , function(error, result){
  if (!error)
    console.log(result);
});

</code></pre>

<p>可以像<a href="#web3ethfilter">web3.eth.filters</a>这样使用事件，他们有相同的方法，但需要传递不同的对象来创建事件过滤器。</p>
<h3 id="_83">参数</h3>
<ol>
<li><code>Object</code> - 想要过滤的索引值，如 <code>{'valueA': 1, 'valueB': [myFirstAddress, mySecondAddress]}</code>. 默认情况下，所以有过滤项被设置为 <code>null</code>. 意味着默认匹配合约所有的日志。</li>
<li><code>Object</code> - 附加的过滤选项, 参考 <a href="#web3ethfilter">filters</a> 第一个参数. 默认情况下，这个对象会设置address为当前合约地址，同时第一个主题为事件的签名。</li>
<li><code>Function</code> - (可选) 传入一个回调函数，将立即开始监听，不用再主动调用 <code>myEvent.watch(function(){})</code>。</li>
</ol>
<h3 id="_84">回调函数返回值</h3>
<p><code>Object</code> - 返回事件对象:</p>
<ul>
<li><code>address</code>: <code>String</code>, 32 字节 - 日志产生的合约地址</li>
<li><code>args</code>: <code>Object</code> - 事件的参数</li>
<li><code>blockHash</code>: <code>String</code>, 32 字节 - 日志所在块的哈希。如果是pending的日志，则为null。</li>
<li><code>blockNumber</code>: <code>Number</code> - 日志所在块的块号。如果是pending的日志，则为null。</li>
<li><code>logIndex</code>: <code>Number</code> -  日志在区块中的序号。</li>
<li><code>event</code>: <code>String</code> - 事件名称</li>
<li><code>removed</code>: <code>bool</code> -  标示产生事件的交易是否被移除 (因为孤块) 或从未生效 (因为交易被拒绝).</li>
<li><code>transactionIndex</code>: <code>Number</code> - 产生日志的交易在区块中的序号。</li>
<li><code>transactionHash</code>: <code>String</code>, 32 字节 - 产生日志的交易哈希值。</li>
</ul>
<h3 id="_85">示例</h3>
<pre><code class="js">var MyContract = web3.eth.contract(abi);
var myContractInstance = MyContract.at('0x78e97bcc5b5dd9ed228fed7a4887c0d7287344a9');

// watch for an event with {some: 'args'}
var myEvent = myContractInstance.MyEvent({some: 'args'}, {fromBlock: 0, toBlock: 'latest'});
myEvent.watch(function(error, result){
   ...
});

// would get all past logs again.
var myResults = myEvent.get(function(error, logs){ ... });

...

// would stop and uninstall the filter
myEvent.stopWatching();
</code></pre>

<hr />
<h3 id="contract-allevents">Contract allEvents</h3>
<pre><code class="js">var events = myContractInstance.allEvents([additionalFilterObject]);

// watch for changes
events.watch(function(error, event){
  if (!error)
    console.log(event);
});

// Or pass a callback to start watching immediately
var events = myContractInstance.allEvents([additionalFilterObject,] function(error, log){
  if (!error)
    console.log(log);
});

</code></pre>

<p>可监听合约触发的所有事件的回调。</p>
<h3 id="_86">参数</h3>
<ol>
<li><code>Object</code> - 附加的过滤选项，参考 <a href="#web3ethfilter">filters</a> 第一个参数. 默认情况下，这个对象会设置address为当前合约地址，主题为事件的签名，不支持额外的主题。</li>
<li><code>Function</code> - (可选) 传入一个回调函数，将立即开始监听，不用再主动调用 <code>myEvent.watch(function(){})</code>。</li>
</ol>
<h3 id="_87">回调函数返回值</h3>
<p><code>Object</code> - 参考上面<a href="#contract-events">合约事件</a> 返回值</p>
<h3 id="_88">示例</h3>
<pre><code class="js">var MyContract = web3.eth.contract(abi);
var myContractInstance = MyContract.at('0x78e97bcc5b5dd9ed228fed7a4887c0d7287344a9');

// watch for an event with {some: 'args'}
var events = myContractInstance.allEvents({fromBlock: 0, toBlock: 'latest'});
events.watch(function(error, result){
   ...
});

// would get all past logs again.
events.get(function(error, logs){ ... });

...

// would stop and uninstall the filter
events.stopWatching();
</code></pre>

<hr />
<h2 id="web3ethgetcompilers">web3.eth.getCompilers</h2>
<pre><code>web3.eth.getCompilers([callback])
</code></pre>
<p>获取可用编译器，不过方法被弃用 https://github.com/ethereum/EIPs/issues/209</p>
<h3 id="_89">参数</h3>
<ol>
<li><code>Function</code> - (可选) 回调函数</li>
</ol>
<h3 id="_90">返回值</h3>
<p><code>Array</code> - 可用编译器列表</p>
<h3 id="_91">示例</h3>
<pre><code class="js">var number = web3.eth.getCompilers();
console.log(number); // [&quot;lll&quot;, &quot;solidity&quot;, &quot;serpent&quot;]
</code></pre>

<hr />
<h2 id="web3ethcompilesolidity">web3.eth.compile.solidity</h2>
<pre><code>web3.eth.compile.solidity(sourceString [, callback])
</code></pre>
<p>编译solidity源码</p>
<h3 id="_92">参数</h3>
<ol>
<li><code>String</code> - solidity源码</li>
<li><code>Function</code> - (optional) 回调函数</li>
</ol>
<h3 id="_93">返回值</h3>
<p><code>Object</code> - 合约及编译信息</p>
<h3 id="_94">示例</h3>
<pre><code class="js">var source = &quot;&quot; +
    &quot;contract test {\n&quot; +
    &quot;   function multiply(uint a) 返回值(uint d) {\n&quot; +
    &quot;       return a * 7;\n&quot; +
    &quot;   }\n&quot; +
    &quot;}\n&quot;;
var compiled = web3.eth.compile.solidity(source);
console.log(compiled);
// {
  &quot;test&quot;: {
    &quot;code&quot;: &quot;0x605280600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b60376004356041565b8060005260206000f35b6000600782029050604d565b91905056&quot;,
    &quot;info&quot;: {
      &quot;source&quot;: &quot;contract test {\n\tfunction multiply(uint a) 返回值(uint d) {\n\t\treturn a * 7;\n\t}\n}\n&quot;,
      &quot;language&quot;: &quot;Solidity&quot;,
      &quot;languageVersion&quot;: &quot;0&quot;,
      &quot;compilerVersion&quot;: &quot;0.8.2&quot;,
      &quot;abiDefinition&quot;: [
        {
          &quot;constant&quot;: false,
          &quot;inputs&quot;: [
            {
              &quot;name&quot;: &quot;a&quot;,
              &quot;type&quot;: &quot;uint256&quot;
            }
          ],
          &quot;name&quot;: &quot;multiply&quot;,
          &quot;outputs&quot;: [
            {
              &quot;name&quot;: &quot;d&quot;,
              &quot;type&quot;: &quot;uint256&quot;
            }
          ],
          &quot;type&quot;: &quot;function&quot;
        }
      ],
      &quot;userDoc&quot;: {
        &quot;methods&quot;: {}
      },
      &quot;developerDoc&quot;: {
        &quot;methods&quot;: {}
      }
    }
  }
}
</code></pre>

<hr />
<h2 id="web3ethcompilelll">web3.eth.compile.lll</h2>
<pre><code>web3.eth.compile.lll(sourceString [, callback])
</code></pre>
<p>编译LLL源码</p>
<h3 id="_95">参数</h3>
<ol>
<li><code>String</code> - LLL源码</li>
<li><code>Function</code> - (可选) 回调函数</li>
</ol>
<h3 id="_96">返回值</h3>
<p><code>String</code> - 编译的LLL HEX代码</p>
<h3 id="_97">示例</h3>
<pre><code class="js">var source = &quot;...&quot;;

var code = web3.eth.compile.lll(source);
console.log(code); // &quot;0x603880600c6000396000f3006001600060e060020a600035048063c6888fa114601857005b6021600435602b565b8060005260206000f35b600081600702905091905056&quot;
</code></pre>

<hr />
<h2 id="web3ethcompileserpent">web3.eth.compile.serpent</h2>
<pre><code>web3.eth.compile.serpent(sourceString [, callback])
</code></pre>
<p>Compiles serpent source code.</p>
<h3 id="_98">参数</h3>
<ol>
<li><code>String</code> - The serpent source code.</li>
<li><code>Function</code> - (optional) If you pass a callback the HTTP request is made asynchronous. See <a href="#using-callbacks">this note</a> for details.</li>
</ol>
<h3 id="_99">返回值</h3>
<p><code>String</code> - The compiled serpent code as HEX string.</p>
<pre><code class="js">var source = &quot;...&quot;;

var code = web3.eth.compile.serpent(source);
console.log(code); // &quot;0x603880600c6000396000f3006001600060e060020a600035048063c6888fa114601857005b6021600435602b565b8060005260206000f35b600081600702905091905056&quot;
</code></pre>

<hr />
<h2 id="web3ethnamereg">web3.eth.namereg</h2>
<pre><code>web3.eth.namereg
</code></pre>
<p>返回GlobalRegistrar对象</p>
<h3 id="usage">Usage</h3>
<p>查看 <a href="https://github.com/ethereum/web3.js/blob/master/示例/namereg.html">namereg</a> 示例.</p>
<hr />
<h2 id="web3ethsendibantransaction">web3.eth.sendIBANTransaction</h2>
<pre><code class="js">var txHash = web3.eth.sendIBANTransaction('0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 'XE81ETHXREGGAVOFYORK', 0x100);
</code></pre>

<p>调用<code>sendIBANTransaction()</code>方法向IBAN地址发送交易.</p>
<h3 id="_100">参数</h3>
<ul>
<li><code>string</code> - 要发送交易的账户地址</li>
<li><code>string</code> - 交易的目标IBAN地址</li>
<li><code>value</code> - 在IBAN交易中要发送的值</li>
</ul>
<hr />
<h2 id="web3ethiban">web3.eth.iban</h2>
<pre><code class="js">var i = new web3.eth.iban(&quot;XE81ETHXREGGAVOFYORK&quot;);
</code></pre>

<hr />
<h3 id="web3ethibanfromaddress">web3.eth.iban.fromAddress</h3>
<pre><code class="js">var i = web3.eth.iban.fromAddress('0x00c5496aee77c1ba1f0854206a26dda82a81d6d8');
console.log(i.toString()); // 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS
</code></pre>

<hr />
<h3 id="web3ethibanfrombban">web3.eth.iban.fromBban</h3>
<pre><code class="js">var i = web3.eth.iban.fromBban('ETHXREGGAVOFYORK');
console.log(i.toString()); // &quot;XE81ETHXREGGAVOFYORK&quot;
</code></pre>

<hr />
<h3 id="web3ethibancreateindirect">web3.eth.iban.createIndirect</h3>
<pre><code class="js">var i = web3.eth.iban.createIndirect({
  institution: &quot;XREG&quot;,
  identifier: &quot;GAVOFYORK&quot;
});
console.log(i.toString()); // &quot;XE81ETHXREGGAVOFYORK&quot;
</code></pre>

<hr />
<h3 id="web3ethibanisvalid">web3.eth.iban.isValid</h3>
<pre><code class="js">var valid = web3.eth.iban.isValid(&quot;XE81ETHXREGGAVOFYORK&quot;);
console.log(valid); // true

var valid2 = web3.eth.iban.isValid(&quot;XE82ETHXREGGAVOFYORK&quot;);
console.log(valid2); // false, cause checksum is incorrect

var i = new web3.eth.iban(&quot;XE81ETHXREGGAVOFYORK&quot;);
var valid3 = i.isValid();
console.log(valid3); // true

</code></pre>

<hr />
<h3 id="web3ethibanisdirect">web3.eth.iban.isDirect</h3>
<pre><code class="js">var i = new web3.eth.iban(&quot;XE81ETHXREGGAVOFYORK&quot;);
var direct = i.isDirect();
console.log(direct); // false
</code></pre>

<hr />
<h3 id="web3ethibanisindirect">web3.eth.iban.isIndirect</h3>
<pre><code class="js">var i = new web3.eth.iban(&quot;XE81ETHXREGGAVOFYORK&quot;);
var indirect = i.isIndirect();
console.log(indirect); // true
</code></pre>

<hr />
<h3 id="web3ethibanchecksum">web3.eth.iban.checksum</h3>
<pre><code class="js">var i = new web3.eth.iban(&quot;XE81ETHXREGGAVOFYORK&quot;);
var checksum = i.checksum();
console.log(checksum); // &quot;81&quot;
</code></pre>

<hr />
<h3 id="web3ethibaninstitution">web3.eth.iban.institution</h3>
<pre><code class="js">var i = new web3.eth.iban(&quot;XE81ETHXREGGAVOFYORK&quot;);
var institution = i.institution();
console.log(institution); // 'XREG'
</code></pre>

<hr />
<h3 id="web3ethibanclient">web3.eth.iban.client</h3>
<pre><code class="js">var i = new web3.eth.iban(&quot;XE81ETHXREGGAVOFYORK&quot;);
var client = i.client();
console.log(client); // 'GAVOFYORK'
</code></pre>

<hr />
<h3 id="web3ethibanaddress">web3.eth.iban.address</h3>
<pre><code class="js">var i = new web3.eth.iban('XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS');
var address = i.address();
console.log(address); // '00c5496aee77c1ba1f0854206a26dda82a81d6d8'
</code></pre>

<hr />
<h3 id="web3ethibantostring">web3.eth.iban.toString</h3>
<pre><code class="js">var i = new web3.eth.iban('XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS');
console.log(i.toString()); // 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS'
</code></pre></div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../js/base.js" defer></script>
        <script src="../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="Search Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Search</h4>
            </div>
            <div class="modal-body">
                <p>
                    From here you can search these documents. Enter
                    your search terms below.
                </p>
                <form role="form">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="Keyboard Shortcuts Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Keyboard Shortcuts</h4>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
